﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using outREACH.App_Code;

namespace outREACH
{
    public partial class EduResourcesView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.IsInRole("admin") || User.IsInRole("NGO") || User.IsInRole("DC"))
            {
                if (Session["UserName"] != null)
                {
                    Label lbl1 = (Label)Master.FindControl("Label1");
                    lbl1.Text = "Welcome " + Session["Username"].ToString() + "!";
                    Label lbl2 = (Label)Master.FindControl("Name");
                    Users u = Users.GetName(Session["Username"].ToString());
                    lbl2.Text = u.firstName + " " + u.lastName;
                    Label lbl3 = (Label)Master.FindControl("Country");
                    lbl3.Text = Session["country"].ToString();
                    Button btn3 = (Button)Master.FindControl("Button4");
                    if (User.IsInRole("admin"))
                    {
                        btn3.Visible = true;
                    }
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }

                string eduResourceID = Request.QueryString["rid"];
                if (IsPostBack != true)
                {
                    if (eduResourceID != null)
                    {
                        List<EduResource> results = EduResource.GetEduResource();
                        foreach (EduResource er in results)
                        {
                            if (Convert.ToInt32(eduResourceID) == er.ER_ID)
                            {
                                id.Text = er.ER_ID.ToString();
                                username.Text = er.userName;
                                rtype.Text = er.resourceType;
                                rname.Text = er.resourceName;
                                quantity.Text = er.quantity.ToString();
                                country.Text = er.country;
                                schname.Text = er.schoolName;
                                date.Text = er.dateTime.ToString();
                                DropDownList1.SelectedValue = "Pending";
                            }
                        }
                    }
                }
                if (eduResourceID != null)
                {
                    List<EduResource> results = EduResource.GetEduResource();
                    foreach (EduResource er in results)
                    {
                        if (Convert.ToInt32(eduResourceID) == er.ER_ID)
                        {

                            sended.Text = er.partial.ToString();
                        }
                    }
                }
                if (User.IsInRole("admin"))
                {
                    DropDownList1.Visible = true;
                    Label5.Visible = false;
                    Button3.Visible = true;
                }
                else
                {
                    TextBox1.Visible = false;
                    DropDownList1.Visible = false;
                    Button3.Visible = false;
                }
                Label5.Text = DropDownList1.SelectedValue.ToString();
            }
            else
            {
                Response.Redirect("~/Education_DCs/EducationResources.aspx");
            }

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue.ToString() == "Partial")
            {
                Label4.Visible = true;
                TextBox1.Visible = true;
            }
            else
            {
                Label4.Visible = false;
                TextBox1.Visible = false;
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string eduResourceID = Request.QueryString["rid"];
            string status = DropDownList1.SelectedValue.ToString();
            int partial = 0;
            if (TextBox1.Text != "")
            {
                partial = Convert.ToInt32(TextBox1.Text);
            }
            else
            {
                partial = 0;
            }
            if (status == "Complete")
            {
                EduResource.updateEduRequest(Convert.ToInt32(eduResourceID), status, 0);
            }
            else
            {
                EduResource.updateEduRequest(Convert.ToInt32(eduResourceID), status, partial);
            }
            sended.Text = partial.ToString();
        }
    }
}
